package com.next

import grails.gorm.transactions.Transactional
import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.*
import org.springframework.security.access.annotation.Secured

@Secured(['ROLE_ADMINISTRATOR'])
class CommunityController
{

    CommunityService communityService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond communityService.list(params), model: [communityCount: communityService.count()]
    }

    def show(Long id)
    {
        respond communityService.get(id)
    }

    def create()
    {
        def param = params
        respond new Community(params)
    }

    def save(Community community)
    {
        if (community == null)
        {
            notFound()
            return
        }

        try
        {
            communityService.save(community)
        }
        catch (ValidationException e)
        {
            respond community.errors, view: 'create'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'community.label', default: 'Community'), community.id])
                redirect community.district
            }
            '*' { respond community.district, [status: CREATED] }
        }
    }

    def edit(Long id)
    {
        respond communityService.get(id)
    }

    def update(Community community)
    {
        if (community == null)
        {
            notFound()
            return
        }

        try
        {
            communityService.save(community)
        }
        catch (ValidationException e)
        {
            respond community.errors, view: 'edit'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'community.label', default: 'Community'), community.id])
                redirect community.district
            }
            '*' { respond community.district, [status: OK] }
        }
    }

    def delete(Long id)
    {
        if (id == null)
        {
            notFound()
            return
        }
        def district = Community.findById(id)?.district
        communityService.delete(id)

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'community.label', default: 'Community'), id])
                redirect district
            }
            '*' { respond community.district, [status: OK] }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'community.label', default: 'Community'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    @Transactional
    def searchCommunity()
    {
        params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0

        def name = params["name"]
        def city = params["city"]
        def district = params["district"]

        String sql = "from Community as u where 1=1"
        if (name)
        {
            sql += " and u.name like '%${name}%'"
        }
        if (city)
        {
            sql += " and u.district.city.name like '%${city}%'"
        }
        if (district)
        {
            sql += " and u.district.name like '%${district}%'"
        }
        def max = params.max
        def offset = params.offset

        def list = Community.findAll(sql, [max: max, offset: offset])
        def list1 = Community.findAll(sql)
        def count = list1.size()

        respond list, model: [communityCount: count, params: params], view: 'index'
    }
}
